package com.sky.service.impl;

import com.sky.service.ReportService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.eventusermodel.XSSFBReader;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private WorkspaceService workspaceService;

    /**
     * 统计报表
     *
     * @param response
     */
    @Override
    @SneakyThrows
    public void businessDateByMonth(HttpServletResponse response) {
        //通过类加载器读取。反射的一种
        File file = new File("D:\\JavaProject_Sky\\New_Sky\\sky-server\\src\\main\\resources\\temaplate/运营数据报表模板.xlsx");
        InputStream inputStream = new FileInputStream(file);
        //查询数据
        //前三十天
        LocalDate begin = LocalDate.now().minusDays(30);
        LocalDate end = LocalDate.now().minusDays(1);
        //获得前三十天内的数据
        BusinessDataVO date = workspaceService.getBusinessData(begin, end);
        //填充数据
        //获得XSSF
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
        xssfWorkbook.getSheetAt(0).getRow(1).getCell(1).setCellValue(begin + "至dao时间的尽头~" + end);
        //将头部填充
        xssfWorkbook.getSheetAt(0).getRow(3).getCell(2).setCellValue(date.getValidOrderCount());
        xssfWorkbook.getSheetAt(0).getRow(3).getCell(4).setCellValue(date.getOrderCompletionRate());
        xssfWorkbook.getSheetAt(0).getRow(3).getCell(6).setCellValue(date.getNewUsers());
        xssfWorkbook.getSheetAt(0).getRow(4).getCell(2).setCellValue(date.getTurnover());
        xssfWorkbook.getSheetAt(0).getRow(4).getCell(4).setCellValue(date.getUnitPrice());

        int num=5;
        int index=1;
        //获取详细信息
        while (!begin.isAfter(end)) {
            BusinessDataVO businessDataVO = workspaceService.getBusinessData(begin,LocalDate.now());

            xssfWorkbook.getSheetAt(0).getRow(num).getCell(index++).setCellValue(begin.toString());
            xssfWorkbook.getSheetAt(0).getRow(num).getCell(index++).setCellValue(businessDataVO.getUnitPrice());
            xssfWorkbook.getSheetAt(0).getRow(num).getCell(index++).setCellValue(businessDataVO.getValidOrderCount());
            xssfWorkbook.getSheetAt(0).getRow(num).getCell(index++).setCellValue(businessDataVO.getOrderCompletionRate());
            xssfWorkbook.getSheetAt(0).getRow(num).getCell(index++).setCellValue(businessDataVO.getTurnover());
            xssfWorkbook.getSheetAt(0).getRow(num).getCell(index).setCellValue(businessDataVO.getNewUsers());

            //时间加一
            begin = begin.plusDays(1);
            num++;
            index=1;
        }

        //通过输出流给他写书去
        ServletOutputStream outputStream = response.getOutputStream();
        xssfWorkbook.write(outputStream);
        outputStream.close();


    }
}
